package ymy;

import org.junit.Test;

import java.lang.reflect.Field;

/**
 * @author com.ymy.hadoop
 * @date 2018/12/15 14:28
 */
public class FiledApp {

    @Test
    public void test01() throws Exception{
        Class<?> clazz = Class.forName("ymy.Student");
        Field[] fields = clazz.getDeclaredFields();
        for(Field field : fields){
            System.out.println(field.getType() + ":" + field.getName());
        }
        System.out.println("--------------");

        Field field = clazz.getDeclaredField("sex");
        System.out.println(field.getType() + ":" + field.getName());
        System.out.println("----------------");
    }

    @Test
    public void test02() throws Exception{
        Class<?> clazz = Class.forName("ymy.Student");
        Field field = clazz.getDeclaredField("sex");
        Student student = (Student) clazz.newInstance();

        if (!field.isAccessible()){
            field.setAccessible(true);
        }

        field.set(student,"男");
        System.out.println(student);
        System.out.println("--------------");

        String sex = (String)field.get(student);
        System.out.println(sex);
    }
}
